//
//  HZMainViewController.swift
//  HansWeibo
//
//  Created by Hans on 16/8/9.
//  Copyright © 2016年 Hans. All rights reserved.
//

import UIKit

class HZMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 实例化自定义tabBar
        let tabBar = HZTabBar()
        
        //设置代理
        tabBar.HZDelegate = self
        
        // 只读属性
        // self.tabBar
        // 通过KVC 给只读属性赋值
        setValue(tabBar, forKey: "tabBar")
        
        //首页
        addChildViewController(HZHomeViewController(), title: "首页", imageName: "tabbar_home")
        //消息
        addChildViewController(HZMassageViewController(), title: "消息", imageName: "tabbar_message_center")
        //发现
        addChildViewController(HZDiscoveryViewController(), title: "发现", imageName: "tabbar_discover")
        //我的
        addChildViewController(HZMyViewController(), title: "我的", imageName: "tabbar_profile")
        
    }

    
    
    func addChildViewController(vc: UIViewController, title: String, imageName: String){
        //设置title
        vc.navigationItem.title = title
        vc.tabBarItem.title = title
        //设置title颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:AppTheneColor], forState: UIControlState.Selected)
        //tabbar_home_selected
        //设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
//        AlwaysOriginal 设置图片渲染模式
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //添加自控制器
        addChildViewController(UINavigationController(rootViewController: vc))
    }
}

extension HZMainViewController: HZTabBarDelegate{
    
    func tabBarButtonClick() {
        print("中间按钮被点击了")
        if !HZUserAccountViewModel.sharedTools.isLogin {
            // 请登录
            return
        }
        
        let composeVc = HZComposeViewController()
        self.presentViewController(UINavigationController(rootViewController:composeVc), animated: true, completion: nil)
        
    }
}


